package com.wenwei.paper.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wenwei.paper.R;
import com.wenwei.paper.bean.DeviceListBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/13.
 */
public class BoxListAdapter extends RecyclerView.Adapter<BoxListAdapter.MyViewHolder> {


    private ArrayList<DeviceListBean> mList;

    public void setDataList(ArrayList<DeviceListBean> list) {
        this.mList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_box_list, null));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        try {
            holder.deviceAddress.setText(mList.get(position).getAddress());
            holder.deviceCode.setText(mList.get(position).getNum() + "");
            holder.deviceSale.setText(mList.get(position).getLane().get(0).getSold_num() == null ? "" : mList.get(position).getLane().get(0).getSold_num());
            holder.deviceSale2.setText(mList.get(position).getLane().get(1).getSold_num() == null ? "" : mList.get(position).getLane().get(1).getSold_num());
            holder.deviceSaleEnd.setText(mList.get(position).getLane().get(0).getSto() + "");
            holder.deviceSaleEnd2.setText(mList.get(position).getLane().get(1).getSto() + "");
            if(mList.get(position).getStatus() == 0){
                holder.deviceState.setVisibility(View.VISIBLE);
                holder.deviceStateImg.setVisibility(View.GONE);
            }else{
                holder.deviceState.setVisibility(View.GONE);
                holder.deviceStateImg.setVisibility(View.VISIBLE);
                if(mList.get(position).getGprs() == 1){
                    holder.deviceStateImg.setImageResource(R.drawable.gps_1);
                }else if(mList.get(position).getGprs() == 2){
                    holder.deviceStateImg.setImageResource(R.drawable.gps_2);
                }else if(mList.get(position).getGprs() == 3){
                    holder.deviceStateImg.setImageResource(R.drawable.gps_3);
                }else if (mList.get(position).getGprs() == 4){
                    holder.deviceStateImg.setImageResource(R.drawable.gps_4);
                }else {
                    holder.deviceStateImg.setImageResource(R.drawable.gps_5);
                }
            }
        } catch (Exception e) {
        }
    }

    private void setImg(int state, ImageView img){
        switch (state){
            case 1:
                img.setImageResource(R.drawable.gps_1);
                break;
            case 2:
                img.setImageResource(R.drawable.gps_2);
                break;
            case 3:
                img.setImageResource(R.drawable.gps_3);
                break;
            case 4:
                img.setImageResource(R.drawable.gps_4);
                break;
            case 5:
                img.setImageResource(R.drawable.gps_5);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.device_code)
        TextView deviceCode;
        @Bind(R.id.device_address)
        TextView deviceAddress;
        @Bind(R.id.device_sale)
        TextView deviceSale;
        @Bind(R.id.device_sale_end)
        TextView deviceSaleEnd;
        @Bind(R.id.device_sale_2)
        TextView deviceSale2;
        @Bind(R.id.device_sale_end_2)
        TextView deviceSaleEnd2;
        @Bind(R.id.device_state)
        TextView deviceState;
        @Bind(R.id.device_state_img)
        ImageView deviceStateImg;
        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }



}
